<?php namespace App\Admin\Access\Role;

trait Service {
    
    //基础服务
    use \App\Core\Service\Service;
    use \App\Core\Service\Services\Indexer;
    use \App\Core\Service\Services\Creater;
    use \App\Core\Service\Services\Updater;
    use \App\Core\Service\Services\Changer;
    use \App\Core\Service\Services\Deleter;
    use \App\Core\Service\Services\Detailer;
    //服务
    use Services\OperationManage;
    //操作日志
    use \App\Admin\Base\LogService;
    
    protected function bootService(){
        $this->bootBaseService();
        $this->setRepository(new Repository);
        $this->setMeta(new Meta);
        $this->repository = $this->getRepository();
        $this->repository->addCriteria(new Criterias\HasPermission(), '', true);
        //添加观察者
        $this->addListeners([
            'create' => new Listeners\CreateListener, 
            'update' => new Listeners\UpdateListener,
            'delete' => new Listeners\DeleteListener
        ]);
    }
	
    //列表
    public function lists(){
        $this->repository->getQuery()->orderBy('role_id', 'ASC');
        $rolesRaw = $this->repository->all('role_id', 'parent_id', 'sub_count', 'name', 'updated_at');
        return $this->success([
            'count' => $rolesRaw->count(),
            'data' => $this->repository->getTree($rolesRaw)
        ]);
    }
}